<?php

namespace app\api\controller;

use app\common\model\Ad;
use app\common\controller\Api;
use app\common\model\AppDownload;
use app\common\model\AppIndex;
use think\Cache;
use think\Config;
use think\Db;
use think\db\Connection;
use think\Request;

/**
 * 首页接口
 * Class Index
 * @package app\api\controller
 */
class Index extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
	
	/**
	 * 商城首页
	 */
    public function index()
    {
    	$cacheName = get_cache_name().'returnData';
	    //如果是调试模式或者没有缓存数据，查找数据库
	    if(!$returnData = check_bebug_cahce($cacheName)) {
		    $returnData = [
			    'banner' => [],
			    'category' => [],
			    'action' => [],
		    ];
		
		    $time = time();
		    $where = [
			    'ai.position' => ['in', [1,2,3]],
			    'ai.begin_time' => ['elt', $time],
			    'ai.end_time' => ['egt', $time],
		    ];
		
		    //顶部轮播图
		    $indexAds = Db::name('appIndex')->alias('ai')
			    ->join('appClass ac', 'ai.app_class_id = ac.id', 'left')
			    ->where($where)
			    ->field('ai.position, ai.app_image, ai.name, ai.url, ai.param_id, ai.login, ac.ios_class, ac.android_class')
			    ->order(['ai.sort' => 'ASC'])
			    ->select();
		
		
		    if($indexAds) {
			    foreach($indexAds as $ad) {
				    if($ad['position'] == 1) {
					    $returnData['banner'][] = $ad;
				    } elseif($ad['position'] == 2) {
					    $returnData['category'][] = $ad;
				    } elseif($ad['position'] == 3) {
					    $returnData['action'][] = $ad;
				    }
			    }
		    }
		
		    //热门商品
		    $goodsModel = new \app\common\model\Goods();
		    $where = [
			    'is_on_sale' => $goodsModel::IS_ON_SALE_Y,
			    'effective_time' => ['elt', time()],
		    ];
		
		    $fields = 'id, goods_name, original_image, shop_price, market_price, sale_number, exchange_integral, goods_type, open_user_grade, use_point as point, give_balance as sendmoney';
		    $hotGoods = Db::name('goods')
			    ->where($where)
			    ->where('is_hot', '=', $goodsModel::HOT_Y)
			    ->order(['sort' => 'ASC'])
			    ->field($fields)
			    ->limit(12)
			    ->select();
		    if($hotGoods) {
		    	foreach($hotGoods as $k => $v) {
		    		$hotGoods[$k]['tags'] = $goodsModel->getGoodsTags($v['goods_type'], $v['open_user_grade']);
			    }
		    }
		    $returnData['hotGoods'] = $hotGoods;
		
		    //推荐商品
		    $recommendGoods = Db::name('goods')
			    ->where($where)
			    ->where('is_recommend', '=', $goodsModel::RECOMMEND_Y)
			    ->order(['sort' => 'ASC'])
			    ->field($fields)
			    ->limit(20)
			    ->select();
		    if($recommendGoods) {
			    foreach($recommendGoods as $k => $v) {
				    $recommendGoods[$k]['tags'] = $goodsModel->getGoodsTags($v['goods_type'], $v['open_user_grade']);
			    }
		    }
		    $returnData['recommend'] = $recommendGoods;
		    //缓存5分钟
		    Cache::set($cacheName, $returnData, 300);
		
	    }
	    $this->success('请求成功', $returnData);
    }
	
	
	/**
	 * 获取app配置
	 */
	public function getAppConfig(){
		$cacheName = get_cache_name('config');
		if(!$returnData = check_bebug_cahce($cacheName)) {
			//配置数据
			$returnData = Db::name('meridConfig')
				->find();
			
			$mobiltType = input('post.mobileType');
			$version = input('post.version');
			
			if($returnData) {
				foreach ($returnData as $key => $value) {
					//如果ios传过来的版本号跟设置的审核版本号一致，那么是审核版本，否则为正式版本
					if($key == 'appstore_version' && $mobiltType == 'ios'){
						if($value == $version) {
							$value = 0;
						} else {
							$value	 = 1;
						}
					}
					$returnData[$key] = $value;
				}
			}
			
			//公告
			$articleModel = new \app\common\model\Article();
			$notice = $articleModel->getCategoryArticle($articleModel::CATE_NOTICE);
			
			$returnData['notice'] = $notice;
			$returnData['return_no'] = [];
			
			$time = time();
			$where = [
				'ai.position' => 11,
				'ai.begin_time' => ['elt', $time],
				'ai.end_time' => ['egt', $time],
			];
			
			//顶部轮播图
			$returnData['open_ad'] = Db::name('appIndex')->alias('ai')
				->join('appClass ac', 'ai.app_class_id = ac.id', 'left')
				->where($where)
				->field('ai.position, ai.app_image, ai.name, ai.url, ai.param_id, ai.login, ac.ios_class, ac.android_class')
				->order(['ai.sort' => 'ASC'])
				->find();
			
			//缓存5分钟
			Cache::set($cacheName, $returnData, 500);
		}
		
		$this->success('查询成功', $returnData);
	}
	
	/**
	 * 获取公告
	 */
	public function notice(){
		$cacheName = get_cache_name();
		if(!$datas = check_bebug_cahce($cacheName)) {
			$datas = Db::name('article')
				->where(['category_id' => 14, 'status' => 1])
				->order(['sort' => 'asc', 'update_time' => 'desc'])
				->select();
			
			Cache::set($cacheName, $datas, 500);
		}
		
		$this->success('查询成功', $datas);
	}
	
	/**
	 * 版本升级
	 */
	
	public function edition()
	{
		
		//手机类型  iphone  android
		$mobileType = input('mobileType');
		
		if ($mobileType == 'ios') {
			$where['app_type'] = AppDownload::TYPE_IOS;
		} elseif ($mobileType == 'android') {
			$where['app_type'] = AppDownload::TYPE_ANDROID;
		} elseif ($mobileType == 'smallapp') {
			$where['app_type'] = AppDownload::TYPE_SMALL;
		} else {
			$this->error('参数错误');
		}
		
		$cacheName = get_cache_name().$mobileType;
		if(!$dataArray = check_bebug_cahce($cacheName)) {
			$dataArray = Db::name('appDownload')
				->where($where)
				->field('edition as version, url, force, msg')
				->find();
			Cache::set($cacheName, $dataArray, 500);
		}
		
		$this->success('查询成功', $dataArray);
	}
	
	/**
	 * v客
	 *
	 */
	public function vk()
	{
		
		$cacheName = get_cache_name();
		if(!$returnData = check_bebug_cahce($cacheName)) {
			$returnData = [
				'banner' => [],
				'category' => [],
			];
			
			$time = time();
			$where = [
				'ai.position' => ['in', [6,7]],
				'ai.begin_time' => ['elt', $time],
				'ai.end_time' => ['egt', $time],
			];
			
			//顶部轮播图
			$indexAds = Db::name('appIndex')->alias('ai')
				->join('appClass ac', 'ai.app_class_id = ac.id', 'left')
				->where($where)
				->field('ai.position, ai.app_image, ai.name, ai.url, ai.param_id, ai.login, ac.ios_class, ac.android_class')
				->order(['ai.sort' => 'ASC'])
				->select();
			if($indexAds) {
				foreach($indexAds as $ad) {
					if($ad['position'] == 6) {
						$returnData['banner'][] = $ad;
					} elseif($ad['position'] == 7) {
						$returnData['category'][] = $ad;
					}
				}
			}
			
			Cache::set($cacheName, $returnData, 500);
		}
		
		$this->success('请求成功', $returnData);
	}
	
	
	/**
	 * 设备规则
	 * @ApiMethod (GET/POST)
	 * @ApiParams (name="imei", type="string", required=true, description="设备卡1IMEI")
	 * @ApiParams (name="ssid", type="string", required=true, description="SSID")
	 * @ApiParams (name="password", type="string", required=true, description="密码")
	 * @ApiParams (name="online", type="int", required=true, description="在线状态，0：离线，1：在线")
	 * @ApiParams (name="phone", type="string", required=false, description="绑定手机号")
	 * @ApiParams (name="active_time", type="string", required=true, description="激活时间")
	 * @ApiParams (name="end_time", type="string", required=true, description="套餐到期时间")
	 * @ApiParams (name="clientnumber", type="int", required=true, description="连接终端数")
	 * @ApiParams (name="signalbar1", type="int", required=true, description="卡1信号格数")
	 * @ApiParams (name="signalbar2", type="int", required=true, description="卡2信号格数")
	 * @ApiParams (name="batteryPers", type="int", required=true, description="电池电量")
	 * @ApiParams (name="batteryCharging", type="int", required=true, description="充电状态，0：否，1：是")
	 * @ApiParams (name="networkType1", type="int", required=true, description="卡1网络制式")
	 * @ApiParams (name="networkType2", type="int", required=true, description="卡2网络制式")
	 * @ApiParams (name="signalDbm1", type="int", required=true, description="卡1 Dbm值")
	 * @ApiParams (name="signalDbm2", type="int", required=true, description="卡2 Dbm值")
	 * @ApiParams (name="prefSubId", type="int", required=true, description="主卡，0：卡1,1：卡2")
	 * @ApiParams (name="iccid1", type="string", required=false, description="卡1 iccid号")
	 * @ApiParams (name="iccid2", type="string", required=false, description="卡2 iccid号")
	 * @ApiParams (name="uptime", type="string", required=true, description="运行时长，小时数：分钟数：秒数")
	 * @ApiParams (name="app_ver", type="string", required=true, description="App当前版本号，以小数点分隔")
	 * @ApiParams (name="ts", type="int", required=true, description="时间戳")
	 * @ApiParams (name="sign", type="string", required=true, description="签名")
	 * @ApiReturnParams (name="code", type="int", required=true, description="状态码")
	 * @ApiReturnParams (name="msg", type="string", required=true, description="消息提示")
	 * @ApiReturnParams (name="data", type="object", description="扩展数据返回")
	 * @ApiReturnParams (name="data/ssid", type="string", description="wifi名称")
	 * @ApiReturnParams (name="data/passwd", type="string", description="密码")
	 * @ApiReturnParams (name="data/switch_id", type="int", description="切卡")
	 * @ApiReturnParams (name="data/client_num", type="int", description="连接数")
	 * @ApiReturnParams (name="data/limit_ru", type="int", description="上行速率")
	 * @ApiReturnParams (name="data/limit_rd", type="int", description="下行速率")
	 * @ApiReturnParams (name="data/app_ver", type="string", description="app文件版本")
	 * @ApiReturnParams (name="data/url", type="string",  description="app文件地址")
	 * @ApiReturn ({"code":200,"msg":"请求成功","time":"","data":{"ssid":"Wifi","passwd":"123456","switch_id":"2","client_num":"8","app_ver":"1.0.0", "url":""}})
	 */
	
	public function test(){
		$q = '中华人民共和国';
		$url = 'https://translate.google.cn/translate_a/single?client=webapp&sl=zh-CN&tl=zh-TW&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&otf=1&ssel=0&tsel=4&kc=2&tk=94121.523059&q='.urlencode($q);
		echo $url;
		die;
		$str = curlGetInfo($url);
		$this->success('aa', $str);
		
	}
}
